package com.xiaoyi.pdocrlibrary;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import java.util.List;

public class PaddOCRSDK {
    public static final int REQUEST_LOAD_MODEL = 0;
    public static final int REQUEST_RUN_MODEL = 1;
    public static final int RESPONSE_LOAD_MODEL_FAILED = 1;
    public static final int RESPONSE_LOAD_MODEL_SUCCESSED = 0;
    public static final int RESPONSE_RUN_MODEL_FAILED = 3;
    public static final int RESPONSE_RUN_MODEL_SUCCESSED = 2;
    private static final PaddOCRSDK ourInstance = new PaddOCRSDK();
    protected String cpuPowerMode = "";
    protected int cpuThreadNum = 1;
    protected String inputColorFormat = "";
    protected float[] inputMean = new float[0];
    protected long[] inputShape = new long[0];
    protected float[] inputStd = new float[0];
    protected String labelPath = "";
    private Context mContext;
    private OnOcrModelInitListener mOnOcrModelInitListener;
    private OnOcrResultListener mOnOcrResultListener;
    protected String modelPath = "";
    protected Predictor predictor = new Predictor();
    protected Handler receiver = null;
    protected float scoreThreshold = 0.1f;
    protected Handler sender = null;
    protected HandlerThread worker = null;

    public interface OnOcrModelInitListener {
        void result(boolean z, String str);
    }

    public interface OnOcrResultListener {
        void result(boolean z, String str, int i, String str2, Bitmap bitmap, List<OCRResultBean> list);
    }

    public static PaddOCRSDK getInstance() {
        return ourInstance;
    }

    private PaddOCRSDK() {
    }

    public void initModel(Context context, OnOcrModelInitListener onOcrModelInitListener) {
        this.mContext = context;
        this.mOnOcrModelInitListener = onOcrModelInitListener;
        HandlerThread handlerThread = new HandlerThread("Predictor Worker");
        this.worker = handlerThread;
        handlerThread.start();
        this.sender = new Handler(this.worker.getLooper()) {
            /* class com.xiaoyi.pdocrlibrary.PaddOCRSDK.AnonymousClass1 */

            public void handleMessage(Message message) {
                int i = message.what;
                if (i != 0) {
                    if (i == 1) {
                        if (PaddOCRSDK.this.onRunModel()) {
                            PaddOCRSDK.this.receiver.sendEmptyMessage(2);
                        } else {
                            PaddOCRSDK.this.receiver.sendEmptyMessage(3);
                        }
                    }
                } else if (PaddOCRSDK.this.onLoadModel()) {
                    PaddOCRSDK.this.receiver.sendEmptyMessage(0);
                } else {
                    PaddOCRSDK.this.receiver.sendEmptyMessage(1);
                }
            }
        };
        this.receiver = new Handler() {
            /* class com.xiaoyi.pdocrlibrary.PaddOCRSDK.AnonymousClass2 */

            public void handleMessage(Message message) {
                int i = message.what;
                if (i == 0) {
                    PaddOCRSDK.this.onLoadModelSuccessed();
                } else if (i == 1) {
                    PaddOCRSDK.this.onLoadModelFailed();
                } else if (i == 2) {
                    PaddOCRSDK.this.onRunModelSuccessed();
                } else if (i == 3) {
                    PaddOCRSDK.this.onRunModelFailed();
                }
            }
        };
        this.modelPath = "models/ocr_v2_for_cpu";
        this.labelPath = "labels/ppocr_keys_v1.txt";
        this.cpuThreadNum = 4;
        this.cpuPowerMode = "LITE_POWER_HIGH";
        this.inputColorFormat = "BGR";
        this.inputShape = PaddleUtils.parseLongsFromString("1,3,960", ",");
        this.inputMean = PaddleUtils.parseFloatsFromString("0.485, 0.456, 0.406", ",");
        this.inputStd = PaddleUtils.parseFloatsFromString("0.229,0.224,0.225", ",");
        this.scoreThreshold = Float.parseFloat("0.1");
        loadModel();
    }

    public void loadModel() {
        this.sender.sendEmptyMessage(0);
    }

    public void runModel() {
        this.sender.sendEmptyMessage(1);
    }

    public boolean onLoadModel() {
        return this.predictor.init(this.mContext, this.modelPath, this.labelPath, this.cpuThreadNum, this.cpuPowerMode, this.inputColorFormat, this.inputShape, this.inputMean, this.inputStd, this.scoreThreshold);
    }

    public boolean onRunModel() {
        return this.predictor.isLoaded() && this.predictor.runModel();
    }

    public void onLoadModelSuccessed() {
        OnOcrModelInitListener onOcrModelInitListener = this.mOnOcrModelInitListener;
        if (onOcrModelInitListener != null) {
            onOcrModelInitListener.result(true, "模型加载成功");
        }
    }

    public void onLoadModelFailed() {
        OnOcrModelInitListener onOcrModelInitListener = this.mOnOcrModelInitListener;
        if (onOcrModelInitListener != null) {
            onOcrModelInitListener.result(false, "模型加载失败");
        }
    }

    public void onRunModelSuccessed() {
        OnOcrResultListener onOcrResultListener = this.mOnOcrResultListener;
        if (onOcrResultListener != null) {
            onOcrResultListener.result(true, "识别成功", (int) this.predictor.inferenceTime(), this.predictor.outputResultToString(), this.predictor.outputImage(), this.predictor.outputResultList());
        }
    }

    public void onRunModelFailed() {
        OnOcrResultListener onOcrResultListener = this.mOnOcrResultListener;
        if (onOcrResultListener != null) {
            onOcrResultListener.result(false, "识别失败", 0, "", null, null);
        }
    }

    public void resloveBitmap(Bitmap bitmap, OnOcrResultListener onOcrResultListener) {
        this.mOnOcrResultListener = onOcrResultListener;
        if (bitmap == null) {
            if (onOcrResultListener != null) {
                onOcrResultListener.result(false, "bitmap为null", 0, "", null, null);
            }
        } else if (!this.predictor.isLoaded()) {
            OnOcrResultListener onOcrResultListener2 = this.mOnOcrResultListener;
            if (onOcrResultListener2 != null) {
                onOcrResultListener2.result(false, "OCR模型还没加载", 0, "", null, null);
            }
        } else {
            this.predictor.setInputImage(bitmap);
            runModel();
        }
    }

    public void destroy() {
        Predictor predictor2 = this.predictor;
        if (predictor2 != null) {
            predictor2.releaseModel();
        }
        this.worker.quit();
    }
}
